//
//  HTSingerViewController.m
//  HeartTravel
//
//  Created by lanou on 15/10/29.
//  Copyright © 2015年 陈文杰. All rights reserved.
//

#import "HTSingerViewController.h"
#import "PKRequestManager.h"
#import "HTSingerModel.h"
#import "UIImageView+WebCache.h"
#import "HTSongViewController.h"
#import "HTSingerCell.h"

@interface HTSingerViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *mainTableView;
@property (nonatomic, strong) NSMutableArray *dataSourceArray;

@end

@implementation HTSingerViewController
- (NSMutableArray *)dataSourceArray {
    
    if (_dataSourceArray == nil) {
        _dataSourceArray = [NSMutableArray arrayWithCapacity:0];
    }
    return _dataSourceArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.leftButton setBackgroundImage:[UIImage imageNamed:@"return"] forState:(UIControlStateNormal)];
    [self.leftButton addTarget:self action:@selector(handleReturnButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    self.titleLabel.text = self.nameTitle;
    _mainTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 100, SCREEN_WIDTH, SCREEN_HEIGHT - 150)];
    _mainTableView.delegate = self;
    _mainTableView.dataSource = self;
    _mainTableView.rowHeight = 60;
//    _mainTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_mainTableView registerClass:[HTSingerCell class] forCellReuseIdentifier:@"cell"];
    _mainTableView.backgroundColor = [UIColor clearColor];
    UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 70, SCREEN_WIDTH, SCREEN_HEIGHT-120)];
    img.image = [UIImage imageNamed:@"海底.jpg"];
    [self.view addSubview:img];
    
    [self.view addSubview:_mainTableView];
    [self configureData];
}

- (void)configureData {
    
    NSString *urlString = [@"http://v1.ard.tj.itlily.com/ttpod?id=" stringByAppendingFormat:@"%@,%@", self.id_name, @"a=getnewttpod&size=1000&page=1&(null)=%26app%3Dttpod%26v%3Dv7.9.4.2015052918%26uid%3D%26mid%3DiPhone7%252C2%26f%3Df320%26s%3Ds310%26imsi%3D%26hid%3D%26splus%3D8.1.2%26active%3D1%26net%3D2%26openudid%3D6520cfef5c2a548f91e7a2ef2bbb4dcbd65c20a9%26idfa%3DFE06BD4D-0645-40B5-9A51-27E13F83A35E%26utdid%3DVdWOeXwUZRwDAA9QVJCvEyX9%26alf%3D201200%26bundle_id%3Dcom.ttpod.music"];
    [PKRequestManager requestWithType:GET urlString:urlString parDic:nil finish:^(NSData *data) {
        
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:nil];
        for (NSDictionary *modelDic in dict[@"data"]) {
            HTSingerModel *model = [[HTSingerModel alloc] init];
            [model setValuesForKeysWithDictionary:modelDic];
            [self.dataSourceArray addObject:model];
        }
        [self.mainTableView reloadData];
    } error:^(NSError *error) {
        NSLog(@"请求失败");
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataSourceArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    HTSingerCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    HTSingerModel *model = _dataSourceArray[indexPath.row];
    [cell.singerImg sd_setImageWithURL:[NSURL URLWithString:model.pic_url]];
    cell.nameLabel.text = model.singer_name;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    HTSongViewController *songVC = [[HTSongViewController alloc] init];
    HTSingerModel *model = _dataSourceArray[indexPath.row];
    songVC.singer_id = model.singer_id;
    songVC.singer_name = model.singer_name;
    [self.navigationController pushViewController:songVC animated:YES];
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CATransform3D rotation;
    rotation = CATransform3DMakeRotation( (90.0*M_PI)/180, 0.0, 0.7, 0.4);
    rotation.m34 = 1.0/ -600;
    
    cell.layer.shadowColor = [[UIColor blackColor]CGColor];
    cell.layer.shadowOffset = CGSizeMake(10, 10);
    cell.alpha = 0;
    cell.layer.transform = rotation;
    cell.layer.anchorPoint = CGPointMake(0, 0.5);
    
    
    [UIView beginAnimations:@"rotation" context:NULL];
    [UIView setAnimationDuration:0.8];
    cell.layer.transform = CATransform3DIdentity;
    cell.alpha = 1;
    cell.layer.shadowOffset = CGSizeMake(0, 0);
    [UIView commitAnimations];
}

- (void)handleReturnButtonAction:(id)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
